package com.app.business.web.business.property;

import com.app.business.en.Status;
import com.app.business.model.BuyHousePerson;
import com.app.business.model.CarLot;
import com.app.business.model.House;
import com.app.business.model.StorageRoom;
import com.app.business.service.*;
import com.app.system.jpa.PageBean;
import com.app.system.permission.model.DataDict;
import com.app.system.permission.service.DataDictService;
import com.app.system.utils.CollectionUtils;
import com.app.system.utils.ParamUtils;
import com.app.system.utils.dataType.BigDecimalUtils;
import com.app.system.utils.dataType.DateUtils;
import com.app.system.utils.dataType.IntegerUtils;
import com.app.system.utils.dataType.StringUtils;
import com.app.system.utils.exception.Precondition;
import com.app.system.utils.exception.Response;
import com.app.system.utils.page.LayUiUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 办证状态数据管理
 * Created by ddz on 2021/9/8.
 *
 * @author ddz
 */
@Controller
@RequestMapping("/business/property/accreditation/*")
@SuppressWarnings("all")
public class AccreditationController {

    @Autowired
    private ContractService contractService;
    @Autowired
    private DataDictService dataDictService;
    @Autowired
    private HouseService houseService;
    @Autowired
    private CarLotService carLotService;
    @Autowired
    private StorageRoomService storageRoomService;
    @Autowired
    private BuyHousePersonService buyHousePersonService;


    /**
     * 办证住宅数据管理列表
     */
    @RequestMapping("accreditationList")
    @RequiresPermissions("accreditation:accreditationList")
    public void accreditationList() {
    }

    /**
     * 加载办证住宅数据管理列表
     *
     * @return
     */
    @RequestMapping("loadAccreditationListList")
    @ResponseBody
    @RequiresPermissions("accreditation:loadAccreditationListList")
    public Map loadAccreditationListList() throws Exception {
        List<Map> listData = Lists.newArrayList();
        PageBean<House> housePageBean = this.houseService.getTakeHousePageBean();
        List<House> houseList = housePageBean.getDatas();
        Map<String, DataDict> daffMap = this.dataDictService.getMapByParentNameForValue("档案发放状态");
        Map<String, DataDict> bzMap = this.dataDictService.getMapByParentNameForValue("办证情况");
        Map<String, DataDict> houseHoldMap = this.dataDictService.getMapByParentNameForValue("房屋户型");
        Map<String, DataDict> townMap = this.dataDictService.getMapByParentNameForValue("乡镇");

        if (!CollectionUtils.isEmpty(houseList)) {
            Map<String, Object> item = null;
            for (House house : houseList) {
                item = null;
                item = Maps.newHashMap();
                List<BuyHousePerson> buyHousePersonList = this.buyHousePersonService.getBuyHousePersonListByHouseId(house.getId());

                item.put("houseId", house.getId());
                item.put("contractId", house.getContract().getId());
                item.put("zsProjectName", StringUtils.getNotNull(house.getContract().getZsProject().getProjectName()));
                item.put("name", StringUtils.getNotNull(house.getContract().getName()));
                item.put("azProjectName", StringUtils.getNotNull(house.getAzProject().getProjectName()));
                item.put("houseNum", StringUtils.getNotNull(house.getHouseNumStr()));
                item.put("houseHold", StringUtils.getNotNull(house.getHouseHold()));
                item.put("houseHoldStr", StringUtils.dataDictShow(houseHoldMap.get(StringUtils.getNotNull(house.getHouseHold()))));
                item.put("buildArea", BigDecimalUtils.getNotNullStr(house.getBuildArea()));
                item.put("bzStatus", IntegerUtils.getNotNull(house.getBzStatus()));
                item.put("bzStatusStr", StringUtils.dataDictShow(bzMap.get(IntegerUtils.getNotNull(house.getBzStatus()).toString())));
                item.put("bzDate", DateUtils.dateToStringDefault(house.getBzDate()));
                String buyHousePersonName = "";
                String buyHousePersonIdCard = "";
                String buyHousePersonTel = "";
                if (!CollectionUtils.isEmpty(buyHousePersonList)) {
                    for (BuyHousePerson buyHousePerson : buyHousePersonList) {
                        if (buyHousePersonList.size() - 1 == buyHousePersonList.indexOf(buyHousePerson)) {

                            buyHousePersonName = buyHousePersonName + buyHousePerson.getName();
                            buyHousePersonIdCard = buyHousePersonIdCard + buyHousePerson.getIdCard();
                            buyHousePersonTel = buyHousePersonIdCard + buyHousePerson.getTel();
                        } else {
                            buyHousePersonName = buyHousePersonName + buyHousePerson.getName() + "、";
                            buyHousePersonIdCard = buyHousePersonIdCard + buyHousePerson.getIdCard() + "、";
                            buyHousePersonTel = buyHousePersonIdCard + buyHousePerson.getTel() + "、";
                        }
                    }
                }
                item.put("buyHousePersonName", StringUtils.getNotNull(buyHousePersonName));
                item.put("buyHousePersonIdCard", StringUtils.getNotNull(buyHousePersonIdCard));
                item.put("buyHousePersonTel", StringUtils.getNotNull(buyHousePersonTel));

                listData.add(item);
            }
        }
        return LayUiUtils.page(housePageBean.getTotalCount().intValue(), listData);
    }

    /**
     * 设置房源办证状态（可撤销）
     *
     * @param request
     */
    @RequestMapping("setBzStatus")
    @ResponseBody
    @RequiresPermissions("accreditation:setBzStatus")
    public synchronized Response setBzStatus(HttpServletRequest request) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Integer houseId = ParamUtils.getInt(request, "houseId", 0);
        House house = this.houseService.findOne(houseId);
        Precondition.checkAjaxArguement(house != null, "1111", "数据错误！");

        if (house.getBzStatus().equals(Status.NO.getIndex())) {
            house.setBzStatus(Status.YES.getIndex());
            house.setBzDate(new Date());
        } else {
            house.setBzStatus(Status.NO.getIndex());
            house.setBzDate(new Date());
        }
        this.houseService.save(house);
        this.contractService.setContractBzStatus(house.getContract());
        return new Response("0001", "操作成功");
    }


    /**
     * 储物间交房数据管理列表
     */
    @RequestMapping("accreditationStorageRoomList")
    @RequiresPermissions("accreditation:accreditationStorageRoomList")
    public void accreditationStorageRoomList() {
    }

    /**
     * 加载储物间交房数据管理列表
     *
     * @return
     */
    @RequestMapping("loadBzStorageRoomList")
    @ResponseBody
    @RequiresPermissions("accreditation:loadBzStorageRoomList")
    public Map loadBzStorageRoomList() throws Exception {
        List<Map> listData = Lists.newArrayList();
        PageBean<StorageRoom> storageRoomPageBean = this.storageRoomService.getDaffStorageRoomPageBean();
        List<StorageRoom> storageRoomList = storageRoomPageBean.getDatas();
        Map<String, DataDict> bzMap = this.dataDictService.getMapByParentNameForValue("办证情况");
        Map<String, DataDict> townMap = this.dataDictService.getMapByParentNameForValue("乡镇");

        if (!CollectionUtils.isEmpty(storageRoomList)) {
            Map<String, Object> item = null;
            for (StorageRoom storageRoom : storageRoomList) {
                item = null;
                item = Maps.newHashMap();
                List<BuyHousePerson> buyHousePersonList = this.buyHousePersonService.getBuyHousePersonListByStorageRoomId(storageRoom.getId());

                item.put("storageRoomId", storageRoom.getId());
                item.put("contractId", storageRoom.getContract().getId());
                item.put("zsProjectName", StringUtils.getNotNull(storageRoom.getContract().getZsProject().getProjectName()));
                item.put("name", StringUtils.getNotNull(storageRoom.getContract().getName()));
                item.put("azProjectName", StringUtils.getNotNull(storageRoom.getAzProject().getProjectName()));
                item.put("houseNum", StringUtils.getNotNull(storageRoom.getHouseNumStr()));
                item.put("buildArea", BigDecimalUtils.getNotNullStr(storageRoom.getBuildArea()));
                item.put("bzStatus", IntegerUtils.getNotNull(storageRoom.getBzStatus()));
                item.put("bzStatusStr", StringUtils.dataDictShow(bzMap.get(IntegerUtils.getNotNull(storageRoom.getBzStatus()).toString())));
                item.put("bzDate", DateUtils.dateToStringDefault(storageRoom.getBzDate()));
                String buyHousePersonName = "";
                String buyHousePersonIdCard = "";
                String buyHousePersonTel = "";
                if (!CollectionUtils.isEmpty(buyHousePersonList)) {
                    for (BuyHousePerson buyHousePerson : buyHousePersonList) {
                        if (buyHousePersonList.size() - 1 == buyHousePersonList.indexOf(buyHousePerson)) {

                            buyHousePersonName = buyHousePersonName + buyHousePerson.getName();
                            buyHousePersonIdCard = buyHousePersonIdCard + buyHousePerson.getIdCard();
                            buyHousePersonTel = buyHousePersonIdCard + buyHousePerson.getTel();
                        } else {
                            buyHousePersonName = buyHousePersonName + buyHousePerson.getName() + "、";
                            buyHousePersonIdCard = buyHousePersonIdCard + buyHousePerson.getIdCard() + "、";
                            buyHousePersonTel = buyHousePersonIdCard + buyHousePerson.getTel() + "、";
                        }
                    }
                }
                item.put("buyHousePersonName", StringUtils.getNotNull(buyHousePersonName));
                item.put("buyHousePersonIdCard", StringUtils.getNotNull(buyHousePersonIdCard));
                item.put("buyHousePersonTel", StringUtils.getNotNull(buyHousePersonTel));

                listData.add(item);
            }
        }
        return LayUiUtils.page(storageRoomPageBean.getTotalCount().intValue(), listData);
    }

    /**
     * 设置储物间交房状态（可撤销）
     *
     * @param request
     */
    @RequestMapping("setBzStorageRoomStatus")
    @ResponseBody
    @RequiresPermissions("accreditation:setBzStorageRoomStatus")
    public synchronized Response setBzStorageRoomStatus(HttpServletRequest request) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Integer storageRoomId = ParamUtils.getInt(request, "storageRoomId", 0);
        StorageRoom storageRoom = this.storageRoomService.findOne(storageRoomId);
        Precondition.checkAjaxArguement(storageRoom != null, "1111", "数据错误！");

        if (storageRoom.getBzStatus().equals(Status.NO.getIndex())) {
            storageRoom.setBzStatus(Status.YES.getIndex());
            storageRoom.setBzDate(new Date());
        } else {
            storageRoom.setBzStatus(Status.NO.getIndex());
            storageRoom.setBzDate(new Date());
        }
        this.storageRoomService.save(storageRoom);
        this.contractService.setContractBzStatus(storageRoom.getContract());
        return new Response("0001", "操作成功");
    }

    /**
     * 交车位数据管理列表
     */
    @RequestMapping("accreditationCarLotList")
    @RequiresPermissions("accreditation:accreditationCarLotList")
    public void accreditationCarLotList() {
    }

    /**
     * 加载交房车位数据管理列表
     *
     * @return
     */
    @RequestMapping("loadBzCarLotList")
    @ResponseBody
    @RequiresPermissions("accreditation:loadBzCarLotList")
    public Map loadBzCarLotList() throws Exception {
        List<Map> listData = Lists.newArrayList();
        PageBean<CarLot> carLotPageBean = this.carLotService.getDaffCarLotPageBean();
        List<CarLot> carLotList = carLotPageBean.getDatas();
        Map<String, DataDict> bzMap = this.dataDictService.getMapByParentNameForValue("办证情况");
        Map<String, DataDict> carLotTypeMap = this.dataDictService.getMapByParentNameForValue("车位类型");
        Map<String, DataDict> townMap = this.dataDictService.getMapByParentNameForValue("乡镇");

        if (!CollectionUtils.isEmpty(carLotList)) {
            Map<String, Object> item = null;
            for (CarLot carLot : carLotList) {
                item = null;
                item = Maps.newHashMap();
                List<BuyHousePerson> buyHousePersonList = this.buyHousePersonService.getBuyHousePersonListByCarLotId(carLot.getId());

                item.put("carLotId", carLot.getId());
                item.put("contractId", carLot.getContract().getId());
                item.put("zsProjectName", StringUtils.getNotNull(carLot.getContract().getZsProject().getProjectName()));
                item.put("name", StringUtils.getNotNull(carLot.getContract().getName()));
                item.put("azProjectName", StringUtils.getNotNull(carLot.getAzProject().getProjectName()));
                item.put("houseNum", StringUtils.getNotNull(carLot.getHouseNumStr()));
                item.put("carLotType", IntegerUtils.getNotNull(carLot.getCarLotType()));
                item.put("carLotTypeStr", StringUtils.dataDictShow(carLotTypeMap.get(IntegerUtils.getNotNull(carLot.getCarLotType()).toString())));
                item.put("buildArea", BigDecimalUtils.getNotNullStr(carLot.getBuildArea()));
                item.put("bzStatus", IntegerUtils.getNotNull(carLot.getBzStatus()));
                item.put("bzStatusStr", StringUtils.dataDictShow(bzMap.get(IntegerUtils.getNotNull(carLot.getBzStatus()).toString())));
                item.put("bzDate", DateUtils.dateToStringDefault(carLot.getBzDate()));

                String buyHousePersonName = "";
                String buyHousePersonIdCard = "";
                String buyHousePersonTel = "";
                if (!CollectionUtils.isEmpty(buyHousePersonList)) {
                    for (BuyHousePerson buyHousePerson : buyHousePersonList) {
                        if (buyHousePersonList.size() - 1 == buyHousePersonList.indexOf(buyHousePerson)) {

                            buyHousePersonName = buyHousePersonName + buyHousePerson.getName();
                            buyHousePersonIdCard = buyHousePersonIdCard + buyHousePerson.getIdCard();
                            buyHousePersonTel = buyHousePersonIdCard + buyHousePerson.getTel();
                        } else {
                            buyHousePersonName = buyHousePersonName + buyHousePerson.getName() + "、";
                            buyHousePersonIdCard = buyHousePersonIdCard + buyHousePerson.getIdCard() + "、";
                            buyHousePersonTel = buyHousePersonIdCard + buyHousePerson.getTel() + "、";
                        }
                    }
                }
                item.put("buyHousePersonName", StringUtils.getNotNull(buyHousePersonName));
                item.put("buyHousePersonIdCard", StringUtils.getNotNull(buyHousePersonIdCard));
                item.put("buyHousePersonTel", StringUtils.getNotNull(buyHousePersonTel));

                listData.add(item);
            }
        }
        return LayUiUtils.page(carLotPageBean.getTotalCount().intValue(), listData);
    }

    /**
     * 设置车位交房状态（可撤销）
     *
     * @param request
     */
    @RequestMapping("setBzCarLotStatus")
    @ResponseBody
    @RequiresPermissions("accreditation:setBzCarLotStatus")
    public synchronized Response setBzCarLotStatus(HttpServletRequest request) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Integer carLotId = ParamUtils.getInt(request, "carLotId", 0);
        CarLot carLot = this.carLotService.findOne(carLotId);
        Precondition.checkAjaxArguement(carLot != null, "1111", "数据错误！");
        if (carLot.getBzStatus().equals(Status.NO.getIndex())) {
            carLot.setBzStatus(Status.YES.getIndex());
            carLot.setBzDate(new Date());
        } else {
            carLot.setBzStatus(Status.NO.getIndex());
            carLot.setBzDate(new Date());
        }
        this.carLotService.save(carLot);
        this.contractService.setContractBzStatus(carLot.getContract());
        return new Response("0001", "操作成功");
    }


    /**
     * 入住管理列表
     */
    @RequestMapping("rzList")
    @RequiresPermissions("accreditation:rzList")
    public void rzList() {
    }

    /**
     * 加载入住管理列表
     *
     * @return
     */
    @RequestMapping("loadRzList")
    @ResponseBody
    @RequiresPermissions("accreditation:loadRzList")
    public Map loadRzListList() {
        List<Map> listData = Lists.newArrayList();
        PageBean<House> housePageBean = this.houseService.getTakeHousePageBean();
        List<House> houseList = housePageBean.getDatas();
        Map<String, DataDict> rzMap = this.dataDictService.getMapByParentNameForValue("入住情况");
        Map<String, DataDict> houseHoldMap = this.dataDictService.getMapByParentNameForValue("房屋户型");
        Map<String, DataDict> townMap = this.dataDictService.getMapByParentNameForValue("乡镇");
        if (!CollectionUtils.isEmpty(houseList)) {
            Map<String, Object> item = null;
            for (House house : houseList) {
                List<BuyHousePerson> buyHousePersonList = this.buyHousePersonService.getBuyHousePersonListByHouseId(house.getId());

                item = null;
                item = Maps.newHashMap();
                item.put("houseId", house.getId());
                item.put("name", StringUtils.getNotNull(house.getContract().getName()));
                item.put("contractCode", StringUtils.getNotNull(house.getContract().getContractCode()));
                item.put("azProjectName", StringUtils.getNotNull(house.getAzProject().getProjectName()));
                item.put("houseNum", StringUtils.getNotNull(house.getHouseNumStr()));
                item.put("houseHold", StringUtils.getNotNull(house.getHouseHold()));
                item.put("houseHoldStr", StringUtils.dataDictShow(houseHoldMap.get(StringUtils.getNotNull(house.getHouseHold()))));
                item.put("rzStatus", IntegerUtils.getNotNull(house.getRzStatus()));
                item.put("rzStatusStr", StringUtils.dataDictShow(rzMap.get(IntegerUtils.getNotNull(house.getRzStatus()).toString())));
                item.put("chooseHouseDate", DateUtils.dateToStringDefault(house.getChooseHouseDate()));
                item.put("rzDate", DateUtils.dateToStringDefault(house.getRzDate()));
                item.put("buildArea", BigDecimalUtils.getNotNullStr(house.getBuildArea()));
                String buyHousePersonName = "";
                String buyHousePersonIdCard = "";
                String buyHousePersonTel = "";
                if (!CollectionUtils.isEmpty(buyHousePersonList)) {
                    for (BuyHousePerson buyHousePerson : buyHousePersonList) {
                        if (buyHousePersonList.size() - 1 == buyHousePersonList.indexOf(buyHousePerson)) {

                            buyHousePersonName = buyHousePersonName + buyHousePerson.getName();
                            buyHousePersonIdCard = buyHousePersonIdCard + buyHousePerson.getIdCard();
                            buyHousePersonTel = buyHousePersonIdCard + buyHousePerson.getTel();
                        } else {
                            buyHousePersonName = buyHousePersonName + buyHousePerson.getName() + "、";
                            buyHousePersonIdCard = buyHousePersonIdCard + buyHousePerson.getIdCard() + "、";
                            buyHousePersonTel = buyHousePersonIdCard + buyHousePerson.getTel() + "、";
                        }
                    }
                }
                item.put("buyHousePersonName", StringUtils.getNotNull(buyHousePersonName));
                item.put("buyHousePersonIdCard", StringUtils.getNotNull(buyHousePersonIdCard));
                item.put("buyHousePersonTel", StringUtils.getNotNull(buyHousePersonTel));
                listData.add(item);
            }
        }
        return LayUiUtils.page(housePageBean.getTotalCount().intValue(), listData);
    }

    /**
     * 设置入住状态（可撤销）
     *
     * @param request
     */
    @RequestMapping("setRzStatus")
    @ResponseBody
    @RequiresPermissions("accreditation:setRzStatus")
    public synchronized Response setRzStatus(HttpServletRequest request) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Integer houseId = ParamUtils.getInt(request, "houseId", 0);
        House house = this.houseService.findOne(houseId);
        Precondition.checkAjaxArguement(house != null, "1111", "数据错误！");

        if (house.getRzStatus().equals(Status.NO.getIndex())) {
            house.setRzStatus(Status.YES.getIndex());
            house.setRzDate(new Date());
        } else {
            house.setRzStatus(Status.NO.getIndex());
            house.setRzDate(null);
        }
        this.houseService.save(house);
        this.contractService.setContractBzStatus(house.getContract());
        return new Response("0001", "操作成功");
    }
}
